// JavaScript Document
//������չ
//parent ����չ����child��չ����child����չparent�е����з���������
Object.extends = function(parent,child){
	for(var i in parent){
		if(typeof parent[i] != 'object'){
			child[i] = parent[i];	
		}	
		else if(parent[i].constructor == Array){
			child[i] = 	Object.clone(parent[i]);
		}
		else{
			child[i] = child[i] || {};
			Object.extendss(parent[i],child[i]);
		}
	}
};
var page = page||{};
//ҳ�湫������
//page�����ṩ���õķ��������湫�õ�����

(function(page){
	/*******************page ����*****************/
	//��ҳ������Ϣ
	/*index:��ʵҳ�룬size:�������url��Ŀ���ַ��method:ajax����ʽ��
	callBack:�ص�������parameters:����*/
	var pagingInfo={
		index:0,
		total:100,
		size:20,
		current:0,
		url:"",
		method:"GET",
		callBack:null,
		parameters:[],
		pagingStyle:{},
	};
	//��ȡ���
	/*url:�����ַ��method������ʽ��parameters������callBack:�ص�����*/
	var loadInfo={
		url:"",
		method:"GET",
		parameters:[],
		callBack:null,
	};
	//�������Ϣ
	/**container:������ĸ�������divide:����㱾�?style:�������ʽ**/
	var divideInfo={
		container:null,
		divide:null,
		divideStyle:{}
	};
	//��ʾ��Ϣ
	/*time:�Զ������ʾ��Ϣ��ʱ��,autoClose���Ƿ��Զ��ر�,hasDivide���Ƿ�
	��Ҫ��������㣬content:��ʾ��Ϣ�ı���container:�����ʾ���������
	Ĭ��Ϊbody��warn����ʾ��warnStyle:��ʾ������ʽ��warnHeaderStyle:
	��ʾ��ͷ��ʽ��warnCenterStyle����ʾ�ı���ʽ*/
	var warnInfo={
		time:2000,
		autoClose:false,
		hasDivide:true,
		content:"�봫���Լ���������Ϣ",
		container:null,
		warn:null,
		warnStyle:{},
		warnHeaderStyle:{},
		warnCenterStyle:{}
	};
	//�����ʾ��Ϣ
	/*time:�Զ������ʾ��Ϣ��ʱ��,equals:��У���Ƿ�Ϊ��ָ����ֵ��Ƚϣ�
		content����Ƚϵ�ֵ��ֻ�ܵ�equalsΪtrueʱ����Ч,
		minLength:�������С���ȣ�maxLength:�������󳤶�*/
	var checkInfo={
		time:3000,
		equals:true,
		content:"",
		minLength:0,
		maxLength:20
	};
	var progressInfo={
		progress:{},
		container:null,
		timer:null,
		length:200,
		progressStyle:{},
		progressInnerStyle:{},
		hasDivide:true
	};
	
	/*****************page ����*******************/
	/**********init method***********/
	//ҳ��������Գ�ʼ��
	var init=function(){
		alert('yes');
	};
	//��ҳ
	var paging=function(option){
		var self = this;
		Object.extendss(option||{},self.pagingInfo);
		var page = self.pagingInfo;
		page.container = page.container || document.body;
		
		var center = self.create("div");
		center.className = "paging";
		Object.extendss(page.pagingStyle,center.style);
		var previous = self.create("span");
		previous.innerHTML = "��һҳ";
		var current = self.create("input");
		current.value = "0";
		current.index = page.current;
		current.total = page.total;
		current.size = page.size;
		current.url = page.url;
		current.parameters = page.parameters;
		current.callBack = page.callBack;
		current.method = page.method;
		var next = self.create("span");
		next.innerHTML = "��һҳ";
		var total = self.create("span");
		total.innerHTML ="�� "+current.total+" ҳ";
		var skip = self.create("span");
		skip.innerHTML = "��ת";
		
		center.appendChild(previous);
		center.appendChild(current);
		center.appendChild(skip);
		center.appendChild(next);
		center.appendChild(total);
		
		page.container.appendChild(center);
		
		previous.onclick = function(){		
			if(current.index==0){
				self.warn({
					autoClose:true,
					hasDivide:false,
					content:"�Ѿ�����ҳ",
					time:800
				});	
				return;
			}
			current.value = --current.index;
			loadInfo();
		};
		next.onclick = function(){
			if(current.total<=current.index){
				self.warn({
					autoClose:true,
					hasDivide:false,
					content:"�Ѿ���ĩҳ",
					time:800
				});	
				return;
			}
			current.value = ++current.index;
			loadInfo();
		};
		skip.onclick = function(){
			if(current.total<current.value){
				self.warn({
					autoClose:true,
					hasDivide:false,
					content:"���"+current.total+"ҳ",
					time:800
				});	
				return;
			}
			current.index = current.value;
			loadInfo();
		}
		function loadInfo(){
			var parameters = "index="+current.index+
							"&size="+current.size;
			for(var i =0;i<current.parameters.length;i++){
				var data = current.parameter[i];
				parameters = parameters + "&"+data+"="+data ;
			}
			ajax.ajaxLoad(current.url,parameters,current.method,current.callBack);	
		}
	};
	//�����
	var progress=function(option){
		var self = this;
		Object.extendss(option||{},self.progressInfo);
		if(self.progressInfo.hasDivide&&!self.divideInfo.divide){
			self.divide();
		}
		var inner = self.create("div");
		with(self.progressInfo){
			container = container || self.divideInfo.divide|| window.document.body;
			progress = self.create("div");
			progress.className = "progress";
			inner.className = "progressInner";
			Object.extendss(progressStyle,progress.style);
			Object.extendss(progressInnerStyle,inner.style);
			progress.appendChild(inner);			
			container.appendChild(progress);
		}
		var length = 0;
		self.progressInfo.timer = setInterval(function(){
			var l = length;
			inner.style.width = length + "px";	
			length = length + 1 ;
			if(length >=self.progressInfo.length){
				length = 0;	
			}
		},10);
	};
	var clear=function(option){
		var self = this;
		if(option.targetName == "divide"){
			if(self.divideInfo.divide){
				self.divideInfo.container.removeChild(self.divideInfo.divide);	
			}
		}
		if(option.targetName == "progress"){
			if(self.divideInfo.divide){
				self.progressInfo.container.removeChild(self.progressInfo.progress);	
			}
		}
		if(option.targetName == "warn"){
			if(self.divideInfo.divide){
				self.warnInfo.container.removeChild(self.warnInfo.warn);	
			}
		}
		
	};
	//�����
	var divide=function(option){
		var self = this;
		Object.extendss(option||{},self.divideInfo);
		with(self.divideInfo){
			divide = self.create("div");
			divide.className = "divide";
			container = container || window.document.body;
			Object.extendss(divideStyle,divide.style);
			container.appendChild(divide);
		}		
	};
	//��ʾ��Ϣչʾ
	var warn=function(option){
		var self = this;
		var center = self.create("span");
		var header = self.create("div");
		Object.extendss(option||{},self.warnInfo);
		if(self.warnInfo.hasDivide&&!self.divideInfo.divide){
			self.divide({});	
		}
		
		with(self.warnInfo){
			container = container || self.divideInfo.divide|| window.document.body;
			warn = self.create("div");
			warn.className = "warn";
			Object.extendss(warnStyle,warn.style);
			Object.extendss(warnHeaderStyle,warn.style);
			Object.extendss(warnCenterStyle,warn.style);
			header.innerHTML = "ϵͳ��ʾ";
			center.innerHTML = content;
			warn.appendChild(header);
			warn.appendChild(center);
			container.appendChild(warn);
		}
		//�Ƿ��Զ��ر�
		if(self.warnInfo.autoClose){
			setTimeout(function(){
				self.warnInfo.container.removeChild(self.warnInfo.warn);					
			},self.warnInfo.time);
		}
		else{
			var cb = self.create("label");	
			cb.innerHTML = "�ر�";
			header.appendChild(cb);
			cb.onclick = function(event){
				var target = self.divideInfo.divide||self.warnInfo.warn;
				document.body.removeChild(target);
			}
		}
	};
	//����У��
	var check=function(option){
		var self = this;
		if(option == null||option=={})return;
		var target = self.get(option.id);
		if(target.tagName != "INPUT"){
			self.warn({time:2,autoClose:true,content:"��������Ϊ�����"});	
			return false;
		}
		var value = target.value;
		var span = self.create("span");
		span.className = "checkNotice";
		with(span.style){
			left = target.offsetLeft + target.offsetWidth + "px";
			top = target.offsetTop + "px";
		}
		Object.extendss(option||{},self.checkInfo);
		setTimeout(function(){
			document.body.removeChild(span);	
		},self.checkInfo.time);
		document.body.appendChild(span);
		
		if(value.length<self.checkInfo.minLength
		   ||value.length>self.checkInfo.maxLength){
			span.innerHTML = "���Ȳ���ȷ("+option.minLength+"��"
			+option.maxLength+"֮��)";
			return false;
		}
		if(self.checkInfo.equals&&value!=self.checkInfo.content){
			span.innerHTML = "��ָ����ֵ��ƥ��";
			return false;
		}
		return true;
	};
	//ͨ��ID��ȡҳ��ڵ�
	var get=function(id){
		return document.getElementById(id);
	};
	//����һ���µı�ǩ�ڵ�
	var create=function(tagName){
		return document.createElement(tagName);
	};
	//��װajax��ȡ��ݶ���
	var ajax = {
		xmlHttp:null,
		createXMLHttpRequest:function(){
			try{
				if(window.ActiveObject){
					this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");	
				}	
				else if(window.XMLHttpRequest){
					this.xmlHttp = new XMLHttpRequest();	
				}
			}	
			catch(e){
				alert(e.name + ":" +e.message);	
			}
		},
		_doGet:function(url,parameter){
			try{
				var rand = Math.round(Math.random()*10000);
				url = url + "?random="+rand+"&"+parameter;
				this.xmlHttp.open("GET",url,false);
				this.xmlHttp.send(null);
			}
			catch(e){
				alert(e.name + ":" +e.message);	
			}
		},
		_doPost:function(url,parameter){
			try{
				this.xmlHttp.open("POST",url,false);
				this.xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
				this.xmlHttp.send(parameter);
			}
			catch(e){
				alert(e.name + ":" +e.message);	
			}
		},
		ajaxLoad:function(url,parameter,method,callBack){
			var self = this;
			self.createXMLHttpRequest();
			if(method.toLowerCase()=="post"){
				self._doPost(url,parameter);
			}
			else{
				self._doGet(url,parameter);	
			}
			self.xmlHttp.onreadystatechange = function(){
				if(	self.xmlHttp.readyState==4){
					if(	self.xmlHttp.status==200){
						var value = self.xmlHttp.responseText;
						callBack(value);	
					}		
				}
			};
			self.xmlHttp.onreadystatechange=null;
		}
	}
})(page);


